package cn.stylefeng.guns.modular.system.controller;

import cn.stylefeng.guns.modular.system.entity.SysTest;
<<<<<<< HEAD
import cn.stylefeng.guns.modular.system.entity.Test;
import cn.stylefeng.guns.modular.system.enums.ResultEnum;
import cn.stylefeng.guns.modular.system.exception.ProjectException;
import cn.stylefeng.guns.modular.system.service.TestService;
import cn.stylefeng.guns.modular.system.utils.ExcelUtils;
import cn.stylefeng.guns.modular.system.utils.ExportExcelZipUtils;
=======
import cn.stylefeng.guns.modular.system.enums.ResultEnum;
import cn.stylefeng.guns.modular.system.exception.ProjectException;
import cn.stylefeng.guns.modular.system.service.TestService;
>>>>>>> 8e1d458d88afe526344a5732c105f8356e021caa
import cn.stylefeng.guns.modular.system.utils.ResultVOUtil;
import cn.stylefeng.guns.modular.system.vo.ResultVO;
import cn.stylefeng.roses.core.base.controller.BaseController;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
<<<<<<< HEAD
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
=======
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
>>>>>>> 8e1d458d88afe526344a5732c105f8356e021caa

@Controller
@RequestMapping("/test")
public class TestController extends BaseController {

    private String PREFIX = "/modular/system/test/";
    @Autowired
    private TestService testService;

<<<<<<< HEAD
    private final static String FILE_NAME = "zipFile";
=======
>>>>>>> 8e1d458d88afe526344a5732c105f8356e021caa

    @RequestMapping("")
    public String index() {
        return PREFIX + "test.html";
    }

<<<<<<< HEAD
    /** zip导出 */
    @GetMapping("/testExpZipExcelFile")
    public void testExpZipExcelFile(HttpServletRequest request, HttpServletResponse response) {
        //第一个文件list
        List<Test> tests1 = Stream.of(Test.builder().id("1").name("1").build(), Test.builder().id("2").name("2").build()).collect(Collectors.toList());
        List<Test> tests2 = Stream.of(Test.builder().id("1").name("1").build(), Test.builder().id("2").name("2").build()).collect(Collectors.toList());
        // 生成的excel表格路径list
        final List<String> fileNameList = new ArrayList<>();
        try {
            ExcelUtils.exportExcel(tests1, "test1", "test1信息", Test.class, "test1", response, request, fileNameList);
            ExcelUtils.exportExcel(tests2, "test2", "test2信息", Test.class, "test2", response, request, fileNameList);
        } catch (IOException e) {
            System.out.println("导出异常");
        }
        final String zipFilePath = ExportExcelZipUtils.toZipFiles(request, fileNameList, FILE_NAME);
        // zip文件创建成功
        ExportExcelZipUtils.download(zipFilePath, FILE_NAME, response);
    }

    /** 导出 */
    @GetMapping("/testExpExcelFile")
    public void testExpExcelFile(HttpServletRequest request, HttpServletResponse response) {
        //第一个文件list
        List<Test> tests1 = Stream.of(Test.builder().id("1").name("1").build(), Test.builder().id("2").name("2").build()).collect(Collectors.toList());
        try {
            ExcelUtils.exportExcel(tests1, "111",  "信息", Test.class, "111", response, request);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 导入
     * @param file
     */
    @RequestMapping(value = "/import", method = RequestMethod.POST)
    public ResultVO importExcel(@RequestParam("file") MultipartFile file) throws IOException {
        if (file.isEmpty())
            throw new IOException("文件为空");
        long start = System.currentTimeMillis();
        List<Test> personVoList = ExcelUtils.importExcel(file, Test.class);
        Optional.of(personVoList.size()).ifPresent(System.out::println);
        long end = System.currentTimeMillis();
        Optional.of("导入excel所花时间："+(end - start)).ifPresent(System.out::println);
        return ResultVOUtil.success("导入成功");
    }

    @ResponseBody
    @GetMapping(value = "/getTest")
    public ResultVO getTest() {
=======
    @ResponseBody
    @GetMapping(value = "/getTest")
    public ResultVO getTest(){
>>>>>>> 8e1d458d88afe526344a5732c105f8356e021caa
        //这是处理异常
//        try {
//            int i = 1 / 0;
//        }catch (ProjectException e){
//            throw new ProjectException(ResultEnum.WHATIS_SITUATION);
//        }
//        SysTest test = testService.getTest(id);
        //设置默认分页参数
<<<<<<< HEAD
        PageHelper.startPage(2, 3);
=======
        PageHelper.startPage(2,3);
>>>>>>> 8e1d458d88afe526344a5732c105f8356e021caa

        List<SysTest> syss = testService.list();

        PageInfo<SysTest> pageInfo = new PageInfo<>(syss);

        List<SysTest> list = pageInfo.getList();
        long total = pageInfo.getTotal();

        return ResultVOUtil.success("");

    }


}
